#!flask/bin/python
import os
import datetime
import unittest
from flask.ext.testing import LiveServerTestCase
from flask_testing import TestCase
from config import LOG_DIR
from golsee import db, createapp
from golsee.models import User, Blog_info, Comments, Role

comment = [{'id': 1,
            'com': ['a', 'a'],
            'child': [{'id': 2,
                       'com': ['b', 'b'],
                       'child': [{'id': 4,
                                  'com': ['d', 'd'],
                                  'child': None},
                                 {'id': 6,
                                  'com': ['f', 'f'],
                                  'child': None}
                                 ]
                       }]
            },
           {'id': 3,
            'com': ['c', 'c'],
            'child': [{'id': 5,
                       'com': ['e', 'e'],
                       'child': None
                       }]
            }
           ]


class MyTestCase(TestCase):
    CSRF_ENABLED = False
    TESTING = True
    SQLALCHEMY_DATABASE_URI = 'mysql://root:zoushj726@192.168.137.2/golsee_test'  # 'sqlite:///' + os.path.join(LOG_DIR, 'test.db')

    def create_app(self):
        app = createapp(self)
        app.config['CSRF_ENABLED'] = self.CSRF_ENABLED
        app.config['TESTING'] = self.TESTING
        app.config['SQLALCHEMY_DATABASE_URL'] = 'mysql://root:zoushj726@192.168.137.2/golsee_test'
        return app

    def setUp(self):
        db.create_all()
        Role.insert_roles()
        for c in comment:
            self.in_comment(c)

    def tearDown(self):
        db.session.remove()
        db.drop_all()



class MyLiveServerTestCase(LiveServerTestCase):
    def create_app(self):
        app = createapp()
        app.config['CSRF_ENABLED'] = False
        app.config['TESTING'] = True
        app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(LOG_DIR, 'test.db')
        app.config['LIVESERVER_PORT'] = 8080
        return app


class TestUser(MyTestCase):
    def test_avatar(self):
        u = User(nicename='john', email='john@example.com')
        avatar = u.avatar(128)
        expected = 'https://secure.gravatar.com/avatar/d4c74594d841139328695756648b6bd6'
        assert avatar[0:len(expected)] == expected

    def test_user_check(self):
        user = User(email='120225883@qq.com', nicename='aaa')
        user.set_password('aaaa')
        db.session.add(user)
        db.session.commit()
        is_true = user.check_password(password='aaaa')
        assert is_true is not False


class TestBlogInfo(MyTestCase):
    date = str(datetime.datetime.now().date())

    def test_get_info_by_day(self):
        old = Blog_info.get_info_by_day(self.date)
        assert old is not None

    def test_new_visit(self):
        old = Blog_info.get_info_by_day(self.date)
        old_visit_day = old.visit_day
        old_visit_all = old.visit_all
        Blog_info.new_visit(self.date)
        new = Blog_info.get_info_by_day(self.date)
        new_visit_day = new.visit_day
        new_visit_all = new.visit_all
        assert old_visit_day + 1 == new_visit_day and old_visit_all + 1 == new_visit_all


class TestComments(MyTestCase):
    def test_tree(self):
        print Comments.get_comment()
        assert 1 == 1


if __name__ == '__main__':
    unittest.main()
